package csel.controller;

import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.Iterator;

import csel.controller.factories.ControllerBuilder;
import csel.controller.listeners.AbstractActionListener;
import csel.controller.listeners.KeyboardListener;
import csel.model.Model;
import csel.view.AreaViewport;
import csel.view.Splasher;
import csel.view.StatusViewport;

/**
 * Reasons for Existence: Uses the ControllerBuilder, ViewPort to set up 
 * the controller and assign action listeners</br>
 * Team Members Responsible</br>
 * Implementation: Nik, Tait</br>
 * Testing: Nik, Tait</br> 
 * 
 * @author Nikolas Wolfe
 */
public class GameController
{
	private Model model;
	private static ControllerBuilder cb; // THIS IS STATIC TO ALLOW DESTRUCTION OF THE CONTROLLER
	private AreaViewport areaViewport;
	private StatusViewport statusViewport;
	private Splasher splashViewport;
	
	
	//****  OUTPUT TOGGLE **************//
	public static boolean infoMode;
	//**********************************//
	
	// Constructor
	public GameController( 
			Model model,
			Splasher splashViewport,
			AreaViewport areaViewport,
			StatusViewport statusViewport,
			boolean infoMode)
	{
		this.model = model;
		this.areaViewport = areaViewport;
		this.statusViewport = statusViewport;
		this.splashViewport = splashViewport;
		cb = new ControllerBuilder(model, splashViewport, areaViewport, statusViewport);
		this.infoMode = infoMode;
	}
	
	public void build()
	{
		// Construct the controller
		cb.contruct();
		
		// Retrieve ActionListers and KeyListeners from ControllerBuilder
		/* 
		 * AreaViewActionListener = A List Item 0
		 */
		ActionListener aval = cb.getAList().get(0); 
		/*
		 * SplashViewActionListener = A List Item 1
		 */
		ActionListener spval = cb.getAList().get(1);
		/*
		 * StatusViewActionListener = A List Item 2
		 */
		ActionListener stval = cb.getAList().get(2);
		/*
		 * KeyboardActionListener = K List Item 0 
		 */
		KeyListener kval = cb.getKList().get(0);
		
		// Add listeners to the view -- TrampolineListener forwards
		// The commands to the appropriate locations
		areaViewport.addActionListener(aval); 	// AreaViewActionListener 
		splashViewport.addActionListener(spval);	// SplashViewActionListener
		statusViewport.addActionListener(stval);	// StatusViewActionListener
		areaViewport.addKeyboardListener(kval);	// KeyboardListener
		statusViewport.addKeyboardListener(kval);
	}
	
	// Kill all threads in the controller, disables all functionality
	// This is only called by the QuitCommand
	public static void killController()
	{
		Iterator<KeyboardListener> i = cb.getKList().iterator();
		while( i.hasNext() )
		{
			i.next().killWorker();
		}
		Iterator<AbstractActionListener> j = cb.getAList().iterator();
		while( j.hasNext() )
		{
			j.next().killWorker();
		}
		cb.getModelDispatcher().killWorker();
		cb.getViewDispatcher().killWorker();
		cb = null;
		System.out.println("[CONTROLLER] Controller destroyed!");
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}
}
